package com.lzx.iteam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.ApprovalBean;
import com.lzx.iteam.bean.ApprovalData;
import com.lzx.iteam.util.DateUtil;
import com.lzx.iteam.util.ImageLoaderInterface;
import com.lzx.iteam.util.StringUtil;
import com.lzx.iteam.widget.RoundImageView;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 04月 08日
 * Time: 下午5:21
 *
 * @QQ : 1234567890
 * **********************************
 */
public class ApprovalDetailAdapter extends BaseAdapter {

    private Context mContext;
    private List<ApprovalBean> mList;

    public ApprovalDetailAdapter(Context mContext, List<ApprovalBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.approval_lv_item_layout,null);
            holder.image = (RoundImageView) convertView.findViewById(R.id.ap_iv_item_head);
            holder.name = (TextView) convertView.findViewById(R.id.ap_item_tv_name);
            holder.state = (TextView) convertView.findViewById(R.id.ap_item_tv_state);
            holder.time = (TextView) convertView.findViewById(R.id.ap_item_tv_time);
            holder.reason = (TextView) convertView.findViewById(R.id.ap_item_tv_reson);
            holder.status = (ImageView) convertView.findViewById(R.id.ap_lv_iv_mid);
            holder.line = convertView.findViewById(R.id.ap_lv_line_botton);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        ApprovalBean bean = mList.get(position);
        if (position == 0 || position == 2){
            holder.status.setImageResource(R.drawable.ap_item_pass_iv);
            holder.state.setTextColor(mContext.getResources().getColor(R.color.ap_detail_pass));
        }else{
            if ("已同意".equals(bean.getStatus())){
                holder.status.setImageResource(R.drawable.ap_item_pass_iv);
                holder.state.setTextColor(mContext.getResources().getColor(R.color.ap_detail_pass));
            }else if ("已拒绝".equals(bean.getStatus())){
                holder.status.setImageResource(R.drawable.ap_item_refause_iv);
                holder.state.setTextColor(mContext.getResources().getColor(R.color.ap_detail_refause));
            }else{
                holder.status.setImageResource(R.drawable.ap_item_wait_iv);
                holder.state.setTextColor(mContext.getResources().getColor(R.color.namecard_note));
            }
        }

        if (mList.size() == 2){
            if (position == 1){
                holder.line.setVisibility(View.GONE);
            }else{
                holder.line.setVisibility(View.VISIBLE);
            }
        }else{
            if (position == 3){
                holder.line.setVisibility(View.GONE);
            }else{
                holder.line.setVisibility(View.VISIBLE);
            }
        }

        holder.name.setText(bean.getName());
        holder.state.setText(bean.getStatus());
        String refuseReason = bean.getRefuseReason();
        if (!StringUtil.isEmpty(refuseReason)){
            holder.reason.setVisibility(View.VISIBLE);
            if (position == 1 || position == 3){
                holder.reason.setText("             （" + refuseReason + "）");
            }else{
                holder.reason.setText("               （" + refuseReason + "）");
            }
        }else{
            holder.reason.setVisibility(View.GONE);
        }
        long time = bean.getTime();
        if (time > 0){
            holder.time.setVisibility(View.VISIBLE);
            holder.time.setText(DateUtil.getApprovalDate(bean.getTime() * 1000));
        }else{
            holder.time.setVisibility(View.GONE);
        }
        ImageLoaderInterface.imageLoader.displayImage(bean.getImage(),holder.image,ImageLoaderInterface.optionHeadImage);
        return convertView;
    }

    class ViewHolder{
        RoundImageView image;
        TextView name,state,time,reason;
        ImageView status;
        View line;
    }
}
